﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web;
using System.Web.Mvc;
namespace Alos.Common
{
    public static class DataHelper
    {
        public static IEnumerable<SelectListItem> ToSelectListItem(this DataTable table, string keyName, string valueName, string selectValue = "")
        {
            return table.Select().Select(x =>
                new SelectListItem
                {
                    Value = x[valueName].ToString(),
                    Text = x[keyName].ToString(),
                    Selected = x[valueName].ToString() == selectValue
                });
        }
        public static IEnumerable<SelectListItem> ToSelectListItem(this DataRow[] table, string keyName, string valueName, string selectValue = "")
        {
            return table.Select(x =>
                new SelectListItem
                {
                    Value = x[valueName].ToString(),
                    Text = x[keyName].ToString(),
                    Selected = x[valueName].ToString() == selectValue
                });
        }
        public static Value GetValue<Key, Value>(this Dictionary<Key, Value> dic, Key key)
        {
            if (dic.ContainsKey(key))
                return dic[key];
            return default(Value);
        }
        public static string ToJson(this DataTable dt)
        {
            if (dt == null || dt.Rows.Count <= 0)
                return "[]";
            return JsonConvert.SerializeObject(dt);
        }
        /// <summary>
        /// 将一个对象写入Session
        /// </summary>
        /// <param name="InfoName"></param>
        /// <param name="InfoValue"></param>
        public static void SetSession(String InfoName, object InfoValue)
        {
            HttpContext.Current.Session[InfoName] = InfoValue;
            if (InfoValue == null) HttpContext.Current.Session.Remove(InfoName);
        }
        public static Object GetObjBySession(String InfoName)
        {
            object _Value = string.Empty;
            if (HttpContext.Current.Session[InfoName] != null)
            {
                _Value = HttpContext.Current.Session[InfoName];
            }
            return _Value;
        }
    }
    public static class FixFunc
    {
        #region Lambda表达式不动点递归
        public static Func<T, TResult> Fix<T, TResult>(Func<Func<T, TResult>, Func<T, TResult>> f)
        {
            return x => f(Fix(f))(x);
        }
        public static Func<T1, T2, TResult> Fix<T1, T2, TResult>(Func<Func<T1, T2, TResult>, Func<T1, T2, TResult>> f)
        {
            return (x, y) => f(Fix(f))(x, y);
        }
        public static Func<T1, T2, T3, TResult> Fix<T1, T2, T3, TResult>(Func<Func<T1, T2, T3, TResult>, Func<T1, T2, T3, TResult>> f)
        {
            return (x, y, z) => f(Fix(f))(x, y, z);
        }
        #endregion
    }

}
